package com.lhj.xxsc.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.xxsc.common.utils.R;
import com.lhj.xxsc.ware.feign.MemperFeignService;
import com.lhj.xxsc.ware.vo.FareVo;
import com.lhj.xxsc.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxsc.common.utils.PageUtils;
import com.xxsc.common.utils.Query;

import com.lhj.xxsc.ware.dao.WareInfoDao;
import com.lhj.xxsc.ware.entity.WareInfoEntity;
import com.lhj.xxsc.ware.service.WareInfoService;
import org.springframework.util.StringUtils;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

	@Autowired
	MemperFeignService memperFeignService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {

		QueryWrapper<WareInfoEntity> wareInfoEntityQueryWrapper = new QueryWrapper<>();
		String key = (String) params.get("key");
		if (!StringUtils.isEmpty(key)) {
			wareInfoEntityQueryWrapper.eq("id", key).or()
					.like("name", key)
					.or().like("address", key)
					.or().like("areacode", key);
		}

		IPage<WareInfoEntity> page = this.page(
				new Query<WareInfoEntity>().getPage(params),
				wareInfoEntityQueryWrapper
		);

		return new PageUtils(page);
	}

	@Override
	public FareVo getFare(Long addrId) {
		BigDecimal addrPrice = new BigDecimal("6");
		FareVo fareVo = new FareVo();
		R r = memperFeignService.getAddr(addrId);
		if (r.getCode() == 0) {
			MemberAddressVo member = r.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>() {
			});
			if (member != null) {
				fareVo.setAddressVo(member);
				//TODO 第三方接口
				String s = member.getPhone().hashCode() + "";
				String addrPriceStr = s.substring(s.length() - 2, s.length() - 1);
				System.out.println(addrPriceStr);
				addrPrice = addrPrice.add(new BigDecimal(addrPriceStr));
			}
		}
		fareVo.setFare(addrPrice);

		return fareVo;
	}

}
